<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" customClass="customWidth" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="任务标题" prop="taskName">
				<el-input v-model="form.taskName" placeholder="任务标题" clearable maxlength="80"></el-input>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="任务类型" prop="taskType">
						<sc-select v-model="form.taskType" :params="{code:'taskType'}" :apiObj="$API.system.sysDic.getSysDicByCode" clearable filterable style="width: 100%"></sc-select>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="任务级别" prop="level">
						<el-radio-group v-model="form.level">
							<el-radio-button label="0">普通</el-radio-button>
							<el-radio-button label="1">重要</el-radio-button>
							<el-radio-button label="2">重点</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="开始时间" prop="beginTime">
						<el-date-picker v-model="form.beginTime" placeholder="开始时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="结束时间" prop="endTime">
						<el-date-picker v-model="form.endTime" placeholder="结束时间" type="date" value-format="YYYY-MM-DD" style="width: 100%;"></el-date-picker>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="任务负责人" prop="chargeAccountId">
						<select-user-dialog v-model:accountId="form.chargeAccountId" :mode="false"/>
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="任务监督人" prop="supervisorAccountId">
						<select-user-dialog v-model:accountId="form.supervisorAccountId" :mode="false"/>
					</el-form-item>
				</el-col>
			</el-row>
			<!-- <el-form-item label="分享人员" prop="userRole">
				<select-user-dialog v-model:accountId="form.userRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="分享部门">
				<select-dept-dialog v-model:deptId="form.deptRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="分享职务">
				<select-user-level-dialog v-model:levelId="form.levelRole" :mode="true"/>
			</el-form-item>
			<el-form-item label="任务参与人">
				<select-user-dialog v-model:accountId="form.participantAccountId" :mode="true"/>
			</el-form-item> -->
			<el-form-item label="任务摘要">
				<sc-editor v-model="form.content" placeholder="请输入" :templates="templates" :height="300" clearable style="width: 100%;"></sc-editor>
			</el-form-item>
			<el-row :gutter="24">
				<el-col :lg="12">
					<el-form-item label="所需工期" prop="duration">
						<el-input-number v-model="form.duration" placeholder="所需工期" clearable></el-input-number>
						/天
					</el-form-item>
				</el-col>
				<el-col :lg="12">
					<el-form-item label="附件权限" prop="attachRole">
						<el-radio-group v-model="form.attachRole">
							<el-radio-button label="1">查看</el-radio-button>
							<el-radio-button label="2">打印</el-radio-button>
							<el-radio-button label="3">下载</el-radio-button>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-form-item label="相关附件" prop="attachId">
				<sc-upload-file :apiObj="apiObj"  v-model="fileList" :data="{module:'task'}" :showFileList="false" @success="addNewFile">
					<el-button type="primary" icon="el-icon-upload">上传附件</el-button>
				</sc-upload-file>
				<attach-menu v-model:attachIds="form.attachId" :roleFlag="4" :preview="true"></attach-menu>
			</el-form-item>
			<msg-box-check-box v-model:msgType="form.msgType" code="task"></msg-box-check-box>
		</el-form>
		<template #footer>
			<el-button @click="visible=false">取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>

</template>
<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import selectDeptDialog from "@/views/module/selectDeptDialog";
import selectUserLevelDialog from "@/views/module/selectUserLevelDialog";
import {defineAsyncComponent} from "vue";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
import AttachMenu from '@/views/module/attachMenu'
const scEditor = defineAsyncComponent(() => import('@/components/scEditor'));
export default {
	emits: ['success', 'closed'],
	components: {
		selectUserLevelDialog,
		msgBoxCheckBox,
		selectUserDialog,
		selectDeptDialog,
		AttachMenu,
		scEditor
	},
	data() {
		return {
			visible: false,
			isSaveing: false,
			isShowMsg: false,
			mode: "add",
			btName: "保存",
			titleMap: {
				add: '发布任务',
				edit: '编辑任务',
				show: '查看'
			},
			fileList: [],
			apiObj:this.$API.file.attach.uploadFile,
			templates: [],
			form: {
				taskId: '',
				taskName: '',
				taskType: '',
				duration: 0,
				level: '0',
				beginTime: '',
				endTime: '',
				participantAccountId:"",
				chargeAccountId: '',
				supervisorAccountId: '',
				userRole: '',
				deptRole: '',
				levelRole: '',
				attachRole: '1',
				content: '',
				msgType: '',
				attachId: ''
			},
			rules: {
				taskName: [
					{required: true, message: '任务标题不能为空', trigger: 'blur'}
				],
				beginTime: [
					{required: true, message: '开始时间不能为空', trigger: 'blur'}
				],
				endTime: [
					{required: true, message: '结束时间不能为空', trigger: 'blur'}
				],
				chargeAccountId: [
					{required: true, message: '任务负责人不能为空', trigger: 'blur'}
				],
				supervisorAccountId: [
					{required: true, message: '任务监督人不能为空', trigger: 'blur'}
				]
			}
		}
	},
	mounted() {
	},
	methods: {
		//显示
		open(mode = 'add') {
			this.mode = mode;
			this.visible = true;
			return this
		},
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.mode == "add") {
						res = await this.$API.clue.task.insertTask.post(this.form);
					} else if (this.mode == "edit") {
						res = await this.$API.clue.task.updateTask.post(this.form);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit('success')
						this.visible = false;
						this.$message.success("操作成功")
					} else {
						this.$alert(res.message, "提示", {type: 'error'})
					}
				}
			})
		},
		setData(data) {
			this.form.taskId = data.taskId
			this.form.taskName = data.taskName
			this.form.taskType = data.taskType
			this.form.duration = data.duration
			this.form.level = data.level
			this.form.beginTime = data.beginTime
			this.form.endTime = data.endTime
			this.form.participantAccountId = data.participantAccountId
			this.form.chargeAccountId = data.chargeAccountId
			this.form.supervisorAccountId = data.supervisorAccountId
			this.form.userRole = data.userRole
			this.form.deptRole = data.deptRole
			this.form.levelRole = data.levelRole
			this.form.attachRole = data.attachRole
			this.form.content = data.content
			this.form.msgType = data.msgType
			this.form.attachId = data.attachId
		},
		addNewFile(res, file)
		{
			let tArr = this.form.attachId.split(",");
			tArr.push(res.data.attachId);
			this.form.attachId = tArr.join(",");
		}
	}
}
</script>

<style>
.customWidth {
	width: 60%;
}
</style>
